package com.exam.demo.service.impl;

import com.exam.demo.mapper.ManagerMapper;
import com.exam.demo.mapper.StudentMapper;
import com.exam.demo.mapper.TeacherMapper;
import com.exam.demo.pojo.*;
import com.exam.demo.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
public class ManagerImpl implements ManagerService {
    @Autowired
    private ManagerMapper managerMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Override
    @Transactional
    //登录方法，返回登录是否成功
    public Boolean login(String manaNo, String manaPwd) {
        Manager manager = this.managerMapper.selectByPrimaryKey(manaNo);
        if(manager.getManaPwd().equals(manaPwd))
            return true;
        else return false;
    }

    @Override
    public Manager getInfo(String manaNo) {
        Manager manager = this.managerMapper.selectByPrimaryKey(manaNo);
        manager.setManaPwd(null);
        return manager;
    }

    @Override
    public List<Student> getStuList(String manaNo) {
        StudentExample studentExample = new StudentExample();
        StudentExample.Criteria criteria = studentExample.createCriteria();
        criteria.andStuNoIsNotNull();
        return this.studentMapper.selectByExample(studentExample);
    }

    @Override
    public List<Teacher> getTeaList(String manaNo) {
        TeacherExample teacherExample = new TeacherExample();
        TeacherExample.Criteria criteria = teacherExample.createCriteria();
        criteria.andTeaNoIsNotNull();
        return this.teacherMapper.selectByExample(teacherExample);
    }

    @Override
    @Transactional
    //管理员修改学生信息，学号不能改
    public int editStuInfo(Student student) {
        return this.studentMapper.updateByPrimaryKeySelective(student);
    }

    @Override
    @Transactional
    //管理员修改老师信息，工号不能改
    public int editTeaInfo(Teacher teacher) {
        return this.teacherMapper.updateByPrimaryKeySelective(teacher);
    }

    @Override
    @Transactional
    //管理员注销学生
    public int deleteStu(String stuNo) {
        return this.studentMapper.deleteByPrimaryKey(stuNo);
    }

    @Override
    @Transactional
    //管理员注销老师
    public int deleteTea(String teaNo) {
        return this.teacherMapper.deleteByPrimaryKey(teaNo);
    }
}
